package com.dummies.android.silentmodetoggle;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import android.R.integer;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
	
	private AudioManager mAudioManager;
	private boolean mPhoneIsSilent;
	private Spinner spnTiempo;
	private Timer timer;
	
    @Override
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Inicialización de servicio de audio
        mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        
        //Revisar estado toggle
        checkIfPhoneIsSilent();
        
        //Evento boton presionado
        setButtonClickListener();
        
        //Debug
        Log.d("Carlos", "This is a test");
        
        initSpinner();
        
        getCurrentTime();
    }
    
    private void initSpinner(){
    	spnTiempo = (Spinner) findViewById(R.id.spinner_tiempo);
    	ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.opciones_tiempo, android.R.layout.simple_spinner_item);
    	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	spnTiempo.setAdapter(adapter);
    }
    
    private void getCurrentTime(){
    	EditText txtHoraActual = (EditText) findViewById(R.id.txtHoraActual);
    	txtHoraActual.setEnabled(false);
    	Calendar calendario = Calendar.getInstance();
        int haHoras = calendario.getTime().getHours();
        int haMinutos = calendario.getTime().getMinutes();
        String horaActual = Integer.toString(haHoras)+":"+Integer.toString(haMinutos);
        txtHoraActual.setText(horaActual);
    }
    
    private void excecuteTask() {
    	timer.cancel();
    	Vibrator vib = (Vibrator) getSystemService(VIBRATOR_SERVICE);
    	vib.vibrate(500);
    	mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    	finish();
    }
    
    //Evento de boton toggle
    private void setButtonClickListener() {
    	Button toggleButton = (Button) findViewById(R.id.toggleButton);
        toggleButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub

				//if(mPhoneIsSilent) {
					//Cambiar a modo silencio
					mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
					mPhoneIsSilent = true;
				//}
				
				String tiempoEspera = (String) spnTiempo.getSelectedItem();
				timer = new Timer();
				timer.schedule
				(
					new TimerTask() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							excecuteTask();
						}
					}
					,
					Integer.parseInt(tiempoEspera)*1000
				);
				//Cambiar imagen UI
				toggleUi();
			}
		});
    }
    
    //Revisar si estado del telef esta en silencio
    private void checkIfPhoneIsSilent() {
    	int ringerMode = mAudioManager.getRingerMode();
    	
    	if(ringerMode == AudioManager.RINGER_MODE_SILENT) {
    		mPhoneIsSilent = true;
    	}
    	else {
    		mPhoneIsSilent = false;
    	}

    }
    
    //Cambia imagen UI de modo silencio a normal y vice versa
    private void toggleUi() {
    	ImageView imageView = (ImageView) findViewById(R.id.phone_icon);
    	Drawable newPhoneImage;
    	
    	if (mPhoneIsSilent) {
    		newPhoneImage = getResources().getDrawable(R.drawable.phoneoff);
    	} 
    	else {
    		newPhoneImage = getResources().getDrawable(R.drawable.phoneon);
    	}
    	
    	imageView.setImageDrawable(newPhoneImage);

    }
    
    protected void onResume() {
    	super.onResume();
    	checkIfPhoneIsSilent();
    	toggleUi();
    }
}